package com.xbongbong.saas.help;

import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2020/5/15 16:14
 * @since v1.0
 */
@Component
public class BatchSeqExplainHelp {

    private static volatile BatchSeqExplainHelp batchSeqExplainHelpCopy;

    private BatchSeqExplainHelp(){};

    public static BatchSeqExplainHelp getInstance() {
        if (batchSeqExplainHelpCopy == null) {
            synchronized (BatchSeqExplainHelp.class) {
                if (batchSeqExplainHelpCopy == null) {
                    batchSeqExplainHelpCopy = new BatchSeqExplainHelp();
                }
            }
        }
        return batchSeqExplainHelpCopy;
    }


    public void formatExplain4BatchSeqAffectNormalAttr(String enable, FieldAttrEntity fieldAttrEntity) {
        if (Objects.equals(enable, BasicConstant.NO_USE.toString())) {
            fieldAttrEntity.setEditHide(1);
            fieldAttrEntity.setVisible(0);
        } else {
            fieldAttrEntity.setEditHide(0);
            fieldAttrEntity.setVisible(1);
        }
    }

    public void formatExplain4BatchAffectProductAttr(List<FieldAttrEntity> fieldAttrEntityList, String enable, String productAttr) {
        List<String> selectProductAttrList = Arrays.asList(SelectProductEnum.BATCH.getAttr(), SelectProductEnum.GUARANTEE_PERIOD.getAttr(), SelectProductEnum.PRODUCE_DATE.getAttr(), SelectProductEnum.EXPIRE_DATE.getAttr());
        for (FieldAttrEntity fieldAttrEntity : fieldAttrEntityList) {
            if (Objects.equals(fieldAttrEntity.getAttr(), productAttr)) {
                List<FieldAttrEntity> subFieldAttrList = (List<FieldAttrEntity>)fieldAttrEntity.getSubForm().getItems();
                for (FieldAttrEntity subField : subFieldAttrList) {
                    if (selectProductAttrList.contains(subField.getAttr())) {
                       formatExplain4BatchSeqAffectNormalAttr(enable,subField);
                    }
                }
                fieldAttrEntity.getSubForm().setItems(subFieldAttrList);
            }
        }
    }

}
